package com.eam.controller;

import com.eam.service.EquipmentDataService;
import com.mes.entity.User;
import com.mes.utils.GenerateCodeUtil;
import com.yy.base.page.Message;
import com.yy.base.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("Troubleshooting")
public class TroubleshootingController {
	@Autowired
	private EquipmentDataService equipmentDataService;
	
/*	//加载故障类型、组织机构
	@ResponseBody
	@RequestMapping(value="devicecoding",method=RequestMethod.POST)
	public Message initProcessInfo(HttpServletRequest request){
		Message msg = new Message();


		//查询故障类型
		msg.setData("faults",equipmentDataService.getEquipmentFaultList());
		//查询车间
		msg.setData("adorgname",equipmentDataService.getEquipmenadorgtList());
		//查询花篮设备
		msg.setData("baskets",equipmentDataService.getFlowerbasket());
		
		
		return msg;
	}*/
	
	/*//根据组织机构加载组织机构下的工序
		@ResponseBody
		@RequestMapping(value="EquipmentProcess",method=RequestMethod.POST)
		public Message EquipmentProcess(HttpServletRequest request){
			String ORGNAME = request.getParameter("ORGNAME");
			Message msg = new Message();
			
			msg.setData("processname",equipmentDataService.getEquipmenprocessList(ORGNAME));
					
			return msg;
		
	}*/
	
	//根据组织机构以及工序加载设备编码
	@ResponseBody
	@RequestMapping(value="Equipmentcode",method=RequestMethod.POST)
	public Message Equipmentcode(HttpServletRequest request){
			
			Message msg = new Message();
			
			//查询故障类型
			msg.setData("faults",equipmentDataService.getEquipmentFaultList());
			
			msg.setData("Plans",equipmentDataService.getEquipmentPlanList());
			return msg;
		
	}		
	
	//根据设备编码加载设备信息
	@ResponseBody
	@RequestMapping(value="EquipmentInformation",method=RequestMethod.POST)
	public Message EquipmentInformation(HttpServletRequest request){
		String code = request.getParameter("CODE");  //设备
		
		Message msg = new Message();
		msg.setData("failices",equipmentDataService.getEquipmentInformationList(code));
		return msg;
	
}

	
	//保存故障报修记录到设备待维修清单页面
	@ResponseBody
	@RequestMapping(value="SavaPendingMaintenanceList",method = RequestMethod.POST)
	public Message SavaPendingMaintenanceList(HttpServletRequest request){
		Message msg = new Message();
		try{
			
		
		String userName = request.getParameter("usernames").toString();  //责任人
		String Epcode = request.getParameter("SBCode").toString();          // 设备编码
		String type = request.getParameter("Equipmenttype").toString(); //维修类型
		String Remarks = request.getParameter("Remarks").toString();   //维修说明的值
		String types = request.getParameter("types").toString();     //故障类型的值
		String effective = request.getParameter("resultstype").toString();  //设备是否停机
		
		
		
		Map<String, Object> faciletiesIds = equipmentDataService.getEquipmentfaciletiesrId(Epcode);
		String faciletiesId = faciletiesIds.get("FACILETIESRID").toString();   //查出设备Id
		String sbname = faciletiesIds.get("SBNAME").toString();   //查出设备名称
		
		
		List<Map<String, Object>> equipmentcounts = equipmentDataService.getEquipmentcounts(faciletiesId);   //设备是否已经报修了
		
		
		
		
			if(equipmentcounts.size() > 0){
				msg.setData("counts","false");
			}else{
				
				User user = UserUtil.getLoginUser(request);
				String adclientId = user.getClientId();   //登录人的ad_client_id
				String userIdtime = user.getId();//登录人的ad_user_id
				String adorgid = user.getOrgId(); //登陆日的ad_org_id
				
				List<Map<String, Object>> ListUser = equipmentDataService.getEquipmentputCreateby(userIdtime);
				
				String createBy = ListUser.get(0).get("CREATEDBY").toString();  //登录人的CreateBy
				
				String updatedBy = ListUser.get(0).get("UPDATEDBY").toString(); //登录人的updateBy
				
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
				java.util.Date time  =sdf.parse(sdf.format(new Date()));  //获取当前系统时间(待维修计划创建时间)
				
				String status = "CREATE";
				String plancode = GenerateCodeUtil.getCode("EMES_Maintain_Plan", "code");  //待维修计划编码
				
						
				Map<String, Object> faults = equipmentDataService.getEquipmentFaultId(types);	

				 String faultId = "";
				 if(faults == null){
					 
				 }else{

				   faultId = faults.get("FAULTID").toString();					//查出故障类型Id
				 } 				
				
				
				equipmentDataService.insertEmesMaintainPlanList(plancode,time,status,adclientId,effective,createBy,updatedBy,type,faultId,Remarks,userIdtime,adorgid);   //将设备报修记录到待维修清单里
		
				equipmentDataService.insertEmesMaintainPlanItemList(faciletiesId,status,effective,adclientId,time,createBy,updatedBy,adorgid); //将报修设备存入到对应计划下
				
			}
		}catch(Exception e){
			msg.setData("results","false");	
			e.printStackTrace();
			
		}
		
		return msg;
	}
	
}